package com.haiyou.dc.mq.listener;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.haiyou.dc.mq.entity.Movie;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * 电影消息消费服务
 *
 * @author huangzl
 */
@Component
@Slf4j
public class MovieConsumerListener {

    @Autowired
    private ObjectMapper objectMapper;

    @RabbitListener(queues = "${mq.basic.info.queue}", containerFactory = "singleListenerContainer")
    public void handleMessage(@Payload byte[] message) throws IOException {
        if (message != null) {
            String result = new String(message, "utf-8");
            log.info("-------------------------------->接收消息={}", result);
            Movie movie = objectMapper.readValue(message, Movie.class);
            log.info("-------------------------------->接收消息={}", movie);
        }
    }

}
